var oNum1 = document.getElementsByClassName('cart-number')[0];
var oNum2 = document.getElementsByClassName('cart-number')[1];
var oNum3 = document.getElementsByClassName('cart-number')[2];
var oNum4 = document.getElementsByClassName('cart-number')[3];
var aInput1 = oNum1.getElementsByTagName('input');
var aInput2 = oNum2.getElementsByTagName('input');
var aInput3 = oNum3.getElementsByTagName('input');
var aInput4 = oNum4.getElementsByTagName('input');
var aAlter = document.getElementsByClassName('cart-alter');
var oPop = document.getElementsByClassName('cart-popover')[0];
var aSku = document.getElementsByClassName('sku-line');
var oDe = document.getElementsByClassName('cart-determine')[0];
var oAb = document.getElementsByClassName('cart-abolish')[0];
var aP = document.getElementsByClassName('cart-price');
var oP1 = aP[0].getElementsByTagName('p')[1];
var oP2 = aP[1].getElementsByTagName('p')[1];
var oP3 = aP[2].getElementsByTagName('p')[1];
var aPp = document.getElementsByClassName('cart-price1');
var oPp1 = aPp[0].getElementsByTagName('p')[0];
var oPp2 = aPp[1].getElementsByTagName('p')[0];
var oPp3 = aPp[2].getElementsByTagName('p')[0];
var oNum = document.getElementsByClassName('cart-box2')[0].getElementsByTagName('em')[0];
var oMoney = document.getElementsByClassName('cart-box3')[0].getElementsByTagName('em')[0];
var oAll = document.getElementById('cart-all');
var aCheck = [];
aCheck[0] = document.getElementsByClassName('cart-checkbox')[0].getElementsByTagName('input')[0];
aCheck[1] = document.getElementsByClassName('cart-checkbox')[1].getElementsByTagName('input')[0];
aCheck[2] = document.getElementsByClassName('cart-checkbox')[2].getElementsByTagName('input')[0];
var oBut = document.getElementsByClassName('cart-box1')[0].getElementsByTagName('span')[0];
var oList = document.getElementsByClassName('cart-list')[0];
var oBot = document.getElementsByClassName('cart-bottom')[0];
var oNull = document.getElementsByClassName('cart-null')[0];
var aUl = document.getElementsByClassName('cart-shop');
var aDis = document.getElementsByClassName('cart-discount');
var aDelete = [];
aDelete[0] = document.getElementsByClassName('cart-action')[0].getElementsByTagName('a')[1];
aDelete[1] = document.getElementsByClassName('cart-action')[1].getElementsByTagName('a')[1];
aDelete[2] = document.getElementsByClassName('cart-action')[2].getElementsByTagName('a')[1];
var oDelete = document.getElementsByClassName('cart-box1')[0].getElementsByTagName('a')[0];

window.onload = function () {
    oAll.checked = false;
    for(i=0;i<aCheck.length;i++){
        aCheck[i].checked=false;
    }
    aInput1[1].value=3;
    aInput2[1].value=3;
    aInput3[1].value=3;
};
//数量可加减
aInput1[0].onclick = function (){
    if(aInput1[1].value>0){
        aInput1[1].value = parseInt(aInput1[1].value)-1;
    }
    oPp1.innerHTML = (oP1.innerHTML*aInput1[1].value).toFixed(2);
    fn();
};
aInput1[2].onclick = function (){
    aInput1[1].value = parseInt(aInput1[1].value)+1;
    oPp1.innerHTML = (oP1.innerHTML*aInput1[1].value).toFixed(2);
    fn();
};
aInput2[0].onclick = function (){
    if(aInput2[1].value>0){
        aInput2[1].value = parseInt(aInput2[1].value)-1;
    }
    oPp2.innerHTML = (oP2.innerHTML*aInput2[1].value).toFixed(2);
    fn();
};
aInput2[2].onclick = function (){
    aInput2[1].value = parseInt(aInput2[1].value)+1;
    oPp2.innerHTML = (oP2.innerHTML*aInput2[1].value).toFixed(2);
    fn();
};
aInput3[0].onclick = function (){
    if(aInput3[1].value>0){
        aInput3[1].value = parseInt(aInput3[1].value)-1;
    }
    oPp3.innerHTML = (oP3.innerHTML*aInput3[1].value).toFixed(2);
    fn();
};
aInput3[2].onclick = function (){
    aInput3[1].value = parseInt(aInput3[1].value)+1;
    oPp3.innerHTML = (oP3.innerHTML*aInput3[1].value).toFixed(2);
    fn();
};
aInput4[0].onclick = function (){
    if(aInput4[1].value>0){
        aInput4[1].value = parseInt(aInput4[1].value)-1;
    }
};
aInput4[2].onclick = function (){
    aInput4[1].value = parseInt(aInput4[1].value)+1;
};

//点击可修改
for(i=0;i<aAlter.length;i++){
    aAlter[i].index = i;
    aAlter[i].onclick = function () {
        oPop.style.display = 'block';
        oPop.style.top = parseInt(100*this.index+200)+'px';
        aSku[0].style = 'border:2px #e01222 solid';
        aSku[2].style = 'border:2px #e01222 solid';
        aSku[1].style = 'border:2px #f5f5f5 solid';
        aSku[3].style = 'border:2px #f5f5f5 solid';
        aSku[0].children[1].style = 'display:block';
        aSku[2].children[1].style = 'display:block';
        aSku[1].children[1].style = 'display:none';
        aSku[3].children[1].style = 'display:none';
        var t = this.parentNode.parentNode.children[5].children[1];
        var one = this.parentNode.parentNode.children[4].children[1];
        var all = this.parentNode.parentNode.children[6].children[0];
        var p = this.parentNode.children;
        aInput4[1].value = t.value;
        for(j=0;j<aSku.length;j++){
            aSku[j].onclick = function () {
                var a = this.parentNode.children;
                for(n=0;n<a.length;n++){
                    a[n].style = 'border:2px #f5f5f5 solid';
                    a[n].children[1].style = 'display:none';
                }
                this.style = 'border:2px #e01222 solid';
                var c = this.children[1];
                c.style = 'display:block';
            };
        }
        oDe.onclick = function () {
            oPop.style.display = 'none';
            t.value = aInput4[1].value;
            all.innerHTML = (one.innerHTML*t.value).toFixed(2);
            fn();
            var arr = [];
            for(j=0;j<aSku.length;j++){
                var c = aSku[j].children[1];
                if(c.style.display === 'block'){
                    arr.push(aSku[j].children[0].innerHTML);
                }
            }
            p[0].innerHTML = '颜色：'+arr[0];
            p[1].innerHTML = '包装：'+arr[1];
        };
        oAb.onclick = function () {
            oPop.style.display = 'none';
        };
    };
}

//全选
oAll.onclick=function(){
    if(oAll.checked){
        for(i=0;i<aCheck.length;i++){
            aCheck[i].checked=true;
        }
    }else{
        for(i=0;i<aCheck.length;i++){
            aCheck[i].checked=false;
        }
    }
    fn();
};
oBut.onclick=function(){
    if(oAll.checked){
        oAll.checked = false;
        for(i=0;i<aCheck.length;i++){
            aCheck[i].checked=false;
        }
    }else{
        oAll.checked = true;
        for(i=0;i<aCheck.length;i++){
            aCheck[i].checked=true;
        }
    }
    fn();
};
aCheck[0].onclick = aCheck[1].onclick = aCheck[2].onclick = function () {
    fn2();
};
function fn2(){
    var arr = [];
    var a = false;
    for(i=0;i<aCheck.length;i++){
        if(aUl[i].style.display != 'none'){
            arr.push(aCheck[i].checked);
        }
    }
    for(i=0;i<arr.length;i++){
        if(arr[i]){
            a = true;
        }else{
            a = false;
            break;
        }
    }
    oAll.checked = a;
    fn();
}

//已选商品数量及总价
function fn() {
    var a = 0.00;
    var b = 0.00;
    if(aCheck[0].checked&&aUl[0].style.display != 'none'){
        a += parseInt(aInput1[1].value);
        b += parseInt(oPp1.innerHTML);
    }
    if(aCheck[1].checked&&aUl[1].style.display != 'none'){
        a += parseInt(aInput2[1].value);
        b += parseInt(oPp2.innerHTML);
    }
    if(aCheck[2].checked&&aUl[2].style.display != 'none'){
        a += parseInt(aInput3[1].value);
        b += parseInt(oPp3.innerHTML);
    }
    oNum.innerHTML = a;
    oMoney.innerHTML = b.toFixed(2);
}

//删除
for(i=0;i<aDelete.length;i++){
    aDelete[i].index = i;
    aDelete[i].onclick = function () {
        aUl[this.index].style.display = 'none';
        fn();
        fn1();
        fn2();
    };
}
oDelete.onclick = function () {
    if(aCheck[0].checked){
        aUl[0].style.display = 'none';
    }
    if(aCheck[1].checked){
        aUl[1].style.display = 'none';
    }
    if(aCheck[2].checked){
        aUl[2].style.display = 'none';
    }
    if(!aCheck[0].checked&&!aCheck[1].checked&&!aCheck[2].checked){
        alert('请选择商品！');
    }
    fn();
    fn1();
};
function fn1() {
    if((aUl[0].style.display == 'none')&&(aUl[1].style.display == 'none')){
        aDis[0].style.display = 'none';
    }
    if(aUl[2].style.display == 'none'){
        aDis[1].style.display = 'none';
    }
    if((aDis[0].style.display == 'none')&&(aDis[1].style.display == 'none')){
        oList.style.display = 'none';
        oBot.style.display = 'none';
        oNull.style.display = 'block';
    }
}